package com.sutuijingling.a;

import android.widget.TextView;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.sutuijingling.utils.c;
import org.apache.http.HttpStatus;

public class f extends e {
    static String a = "UTF7 IME for UI Testing";
    static String b = "默认";

    public f(UiDevice uiDevice) {
        this.device = uiDevice;
        System.out.println(" init setting view device :" + this.device);
    }

    private void a(UiObject uiObject) throws UiObjectNotFoundException {
        c.a(uiObject, 10000);
        System.out.println(" drag to find the chs language input");
        simpleDragScroll(true, 5);
        c.c();
        simpleDragScroll(true, 5);
        c.c();
        simpleDragScroll(true, 5);
        c.a(findScrollText("中文 (简体)", 2), 10000);
        boolean d = d();
        System.out.println(" is still language setting??" + d);
        if (d) {
            this.device.pressBack();
        }
    }

    private boolean d() throws UiObjectNotFoundException {
        UiObject findByResId = findByResId("android:id/action_bar_title", 1);
        if (!findByResId.exists()) {
            return false;
        }
        String text = findByResId.getText();
        return "Language".equals(text) || "语言".equals(text);
    }

    private void e() throws UiObjectNotFoundException, com.sutuijingling.result.c {
        UiObject findText = findText(b, 2);
        if (findText.exists()) {
            c.a(findText, 3000);
            c.d();
            findText = f();
            if (findText.exists()) {
                c.d(findText);
                c.d();
                return;
            }
            this.device.pressBack();
            c.a(f(), 3000);
            findText = findText("确定", 1);
            if (findText.exists()) {
                c.a(findText, 3000);
                c.d();
            }
            c.a(findText(b, 2), 5000);
            c.a(f(), 3000);
            c.c();
            return;
        }
        throw new com.sutuijingling.result.c(HttpStatus.SC_NOT_FOUND, "改不了默认输入法??");
    }

    private UiObject f() {
        UiObject findTextView = findTextView(a, 1);
        if (!findTextView.exists()) {
            findTextView = findText("手机流量专用", 2);
        }
        if (!findTextView.exists()) {
            findTextView = findTextViewContains("UTF7", 1);
        }
        if (!findTextView.exists()) {
            findTextView = findText(a, 2);
        }
        return !findTextView.exists() ? findText("DSConfig", 1) : findTextView;
    }

    private void g() throws com.sutuijingling.result.c, UiObjectNotFoundException {
        UiObject f = f();
        if (!f.exists()) {
            h();
        }
        c.a(f, 2000);
        UiObject findText = findText("确定", 1);
        if (!findText.exists()) {
            c.c(f);
            c.c();
        }
        if (findText.exists()) {
            c.c(findText);
            c.c();
        }
        h();
    }

    private UiObject h() throws com.sutuijingling.result.c, UiObjectNotFoundException {
        UiObject findText = findText("默认", 2);
        if (findText.exists()) {
            c.a(findText, 3000);
            findText = f();
            ensureExist(findText, "没有这个输入法: " + a);
            c.c(findText);
            c.c();
            return findText;
        }
        throw new com.sutuijingling.result.c(HttpStatus.SC_NOT_FOUND, "改不了默认输入法??");
    }

    public void a() {
        System.out.println(" backhome setting view device :" + this.device);
        this.device.pressBack();
        c.b();
        this.device.pressBack();
        c.b();
        this.device.pressBack();
        c.c();
        this.device.pressHome();
        c.c();
        this.device.pressBack();
    }

    public void b() throws com.sutuijingling.result.c, UiObjectNotFoundException {
        a();
        this.device.pressMenu();
        UiObject uiObject = new UiObject(new UiSelector().className(TextView.class).text("设置"));
        if (!uiObject.exists()) {
            uiObject = new UiObject(new UiSelector().className(TextView.class).text("系统设置"));
        }
        if (!uiObject.exists()) {
            uiObject = findTextView("System settings", 2);
        }
        ensureExist(uiObject, "No Menu after system setting click....");
        c.d(uiObject);
        uiObject = findText("语言和输入法", 2);
        if (!uiObject.exists()) {
            uiObject = findTextView("Language & input", 2);
        }
        if (!uiObject.exists()) {
            uiObject = findScroll().getChildByText(new UiSelector().className(TextView.class), "语言和输入法", true);
        }
        c.c();
        ensureExist(uiObject, "找不到 语言和 输入法这个 entry");
        debugUiObject(uiObject);
        c.a(uiObject, 2000);
        c.c();
        uiObject = findTextView("English (United States)", 1);
        if (!uiObject.exists()) {
            uiObject = findTextView("English (United Kingdom)", 1);
        }
        if (uiObject.exists()) {
            a(uiObject);
        }
        uiObject = findText("靠谱输入法", 1);
        if (!uiObject.exists()) {
            uiObject = findTextContain("AOSP", 1);
        }
        if (uiObject.exists()) {
            e();
        } else {
            g();
        }
        this.device.pressBack();
        c.b();
        this.device.pressBack();
        c.b();
        this.device.pressBack();
        c.b();
    }

    public void c() throws UiObjectNotFoundException {
        UiObject findTextView = findTextView("取消", 1);
        if (!findTextView.exists()) {
            findTextView = findText("取消", 1);
        }
        if (!findTextView.exists()) {
            findTextView = findTextView("Cancel", 1);
        }
        if (!findTextView.exists()) {
            findTextView = findText("Cancel", 1);
        }
        if (findTextView.exists()) {
            c.c(findTextView);
        }
    }
}
